package com.hymjweb.demo.hymj.master_bill_manage.load.controller;

import com.alibaba.fastjson.JSONObject;
import com.hymjweb.demo.framework.sys.BaseController;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.master_bill_manage.load.bean.BNativeBillLoadPojo;
import com.hymjweb.demo.hymj.master_bill_manage.load.service.BNativeBillLoadService;
import com.hymjweb.demo.hymj.native_bill_manage.service.BNativeBillService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/pages/master_bill_manage/load")
public class BNativeBillLoadController extends BaseController {

    @Autowired
    private BNativeBillLoadService bNativeBillLoadService;

    @Autowired
    private BNativeBillService bNativeBillService;

    /**
     * 根据主单ID加载主单配载的分单信息
     * @param masterBillId
     * @return
     */
    @RequestMapping("/queryLoadBillByMasterBillId")
    @ResponseBody
    public Object queryLoadBillByMasterBillId(String masterBillId) throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("masterBillId",masterBillId);

        List<BNativeBillLoadPojo> list  = bNativeBillLoadService.queryNativeBillListNoPage(map);

        List<Map<String,Object>> pullLoadList =new ArrayList<>();

        for(BNativeBillLoadPojo pojo : list){
            try {
                Map pojoMap = BeanUtils.describe(pojo);

                // 添加相应的属性
                // 分单拉货件数
                pojoMap.put("pullNumber","0");
                // 分单拉货实重
                pojoMap.put("pullActualWeight","0");
                // 分单拉货计重
                pojoMap.put("pullChargeableWeight","0");
                // 分单拉货体积
                pojoMap.put("pullVolume","0");
                // 分单拉货调整金额
                pojoMap.put("pullAmount","0");
                // 分单拉货备注
                pojoMap.put("pullComment","");

                // 加入列表
                pullLoadList.add(pojoMap);

            } catch (Exception e) {
                throw new Exception("对象转Map失败");
            }
        }

        return pullLoadList;

    }


    /**
     * 根据分单ID查询主单信息
     * @return
     */
    @RequestMapping("/queryMasterBillByNativeBillId")
    @ResponseBody
    public Object queryMasterBillByNativeBillId(String nativeBillId) throws Exception{
        if(DataTypeUtil.isEmptyStr(nativeBillId)){
            throwException("根据分单ID查询主单参数错误!");
        }
        Map<String,Object> param = new HashMap();
        param.put("billId",nativeBillId);
        return bNativeBillLoadService.queryNativeBillList(param);
    }


    /**
     * 根据分单ID查询主单信息  share_gross_profit
     * @return
     */
    @RequestMapping("/queryNativeBillLoadGrossProfit")
    @ResponseBody
    public Object queryNativeBillLoadGrossProfit(@RequestParam(required = false) Map param){
        return bNativeBillLoadService.getPageList(param);
    }

    /**
     * 根据分单ID查询主单信息  share_gross_profit
     * @return
     */
    @RequestMapping("/queryNativeBillLoadProfit")
    @ResponseBody
    public Object queryNativeBillLoadProfit(@RequestParam(required = false) Map param){
        return bNativeBillLoadService.queryNativeBillLoadProfit(param);
    }


    @RequestMapping("/totalCalc")
    @ResponseBody
    public Object totalCalc(@RequestBody JSONObject jsonObject){
        /**
         * "keyBillCode" -> ""
         * "keyOpenBillEndDate" -> ""
         * "keyTransportWayId" -> ""
         * "keyTransportSpeciesId" -> ""
         * "keyOpenBillStartDate" -> ""
         * "keyShareCostCommentLength" -> {Integer@11011} 10
         * "keyDeliveryCustomerId" -> ""
         * "keyAttributionDepartmentId" -> ""
         */

        // totalBillCount,totalNumber,totalActualWeight,totalChargeableWeight,totalAgentDeliveryCharge,totalServiceCharge,totalAmount,totalSettlementAmount,totalShareCost,totalGrossProfit

        Map<String,Object> totalMap = bNativeBillService.totalCalc(jsonObject);
        return totalMap;
    }



}
